# coding=utf-8
"""
Author: Mr.夏末
@Function: 权限验证
"""
import re
from hashlib import md5

# 1.加密
# MD5加密
import requests

password = '123456'.encode('utf8')
password = md5(password).hexdigest()
print(password)

# 2.原生会话管理
session = requests.session()
# 需要登录
print(session.cookies)
res = session.post("http://testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login&t=0.22317188934657084", data={
    'username': '2529085125@qq.com',
    'password': '123456',
    'verify_code': '0'
})
print(res.headers)
print(session.cookies)
res = session.get('http://testingedu.com.cn:8000/Home/User/info.html')
print(re.findall('<title>(.*)</title>', res.text))

# 3.token验证
session = requests.session()
res = session.post("http://www.testingedu.com.cn:8081/inter/HTTP/auth")
print(res.text)
token = res.json().get('token')
session.headers['token'] = token

# 在需要验证的接口带上
p_dict = {
    'username': 'hzs{random}',
    'pwd': '123456',
    'nickname': '1234',
    'describe': '1234'
}
#注册
res = session.post("http://www.testingedu.com.cn:8081/inter/HTTP/register", data=p_dict, headers={'token': token})
print(res.text)
# 登录
res = session.post("http://www.testingedu.com.cn:8081/inter/HTTP/login",
                    data={
                        'username': 'hzs001',
                        'password': '123456'
                    })
print(res.text)
# 查询
res = session.post("http://www.testingedu.com.cn:8081/inter/HTTP/getUserInfo",
                    data={
                        'id': res.json().get('userid'),
                    })
print(res.text)
# 退出登录
res = session.post("http://www.testingedu.com.cn:8081/inter/HTTP/logout")
print(res.text)
